<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Leave_management_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_all_leave()
	{
	$this->db->select('user_leave.*,users.username');
	$this->db->from('user_leave');
	$this->db->join('users', 'user_leave.user_id = users.id');
	//$this->db->join('user_leave_days', 'user_leave_days.user_leave_id = user_leave.id');
	$this->db->order_by('id','DESC');
	return $this->db->get()->result();	
	$this->db->close();
	}
	
	public function get_leave_by_id($id){
		$this->db->select('user_leave.*,users.username,users.id as user_id');
		$this->db->from('user_leave');
		$this->db->where('user_leave.id',$id);
		$this->db->join('users', 'user_leave.user_id = users.id');
		return $this->db->get()->result();	
		$this->db->close();
	}
	
	public function grant_leave($updates,$id)
	{
		// get the details required for attendance		
		$this->db->from('user_leave');
		$this->db->where('id',$id);
		$user_id = $this->db->get()->row()->user_id;

		foreach($updates as $update){		
			foreach($update as $key=>$value){
				
			$this->db->update('user_leave_days',array('status'=>$value),array('id'=>$key));
			if($value=='paid'){
				$this->db->from('user_leave_days');
				$this->db->where('id',$key);
				$date = $this->db->get()->row()->date;
				$d = explode('-',$date);
				$year = trim($d[0],'20');
				$month = $d[1];
				$day = 'day_'.$d[2];
				$pwhere = array(
				 'year_id' 	=> 	$year,
				 'month_id' => 	$month,
				 'user_id' 	=>	$user_id,
				// $day 		=>	'paid_leave',
				);
				
				/* check present or past month */
				$result = $this->db->get_where('daily_attendance_sheet',$pwhere);
				if($result){
					
					/* if present month update the present table*/
					$this->db->update('daily_attendance_sheet',array($day => 'paid_leave'),$pwhere);
					
				}else{
				
				/*get all sundays*/
				$sunday = $this->get_sunday($month,$year);
				
				/* create a new row for that  user*/
				$this->db->insert('daily_attendance_sheet',$pwhere);
						
						/*update all sundays*/
						foreach ($sunday as $s){
						$insert_sunday['day_'.$sundays] = 'sunday';
						$this->db->update('daily_attendance_sheet',$insert_sunday,$pwhere);	
					}
				
				}
			
			}elseif($value=='unpaid'){
				$this->db->from('user_leave_days');
				$this->db->where('id',$key);
				$date = $this->db->get()->row()->date;
				$d = explode('-',$date);
				$year = trim($d[0],'20');
				$month = $d[1];
				$day = 'day_'.$d[2];
						$where = array(
						 'year_id' 	=> 	$year,
						 'month_id' => 	$month,
						 'user_id' 	=>	$user_id,						
						);
						
						/* check present or past month */
						$result = $this->db->get_where('daily_attendance_sheet',$where);
						
						if($result){
					
					/* if present month update the present table*/
					$this->db->update('daily_attendance_sheet',array($day => 'unpaid_leave'),$where);
					
				}else{
				
				/*get all sundays*/
				$sunday = $this->get_sunday($month,$year);
				
				/* create a new row for that  user*/
				$this->db->insert('daily_attendance_sheet',$where);
						
						/*update all sundays*/
						foreach ($sunday as $s){
						$insert_sunday['day_'.$sundays] = 'sunday';
						$this->db->update('daily_attendance_sheet',$insert_sunday,$where);	
							}
				
						}	
						
					}elseif($value=='rejected'){
				$this->db->from('user_leave_days');
				$this->db->where('id',$key);
				$date = $this->db->get()->row()->date;
				$d = explode('-',$date);
				$year = trim($d[0],'20');
				$month = $d[1];
				$day = 'day_'.$d[2];
						$where = array(
						 'year_id' 	=> 	$year,
						 'month_id' => 	$month,
						 'user_id' 	=>	$user_id,						
						);
						
						/* check present or past month */
						$result = $this->db->get_where('daily_attendance_sheet',$where);
						
						if($result){
					
					/* if present month update the present table*/
					$this->db->update('daily_attendance_sheet',array($day => 'rejected'),$where);
					
				}else{
				
				/*get all sundays*/
				$sunday = $this->get_sunday($month,$year);
				
				/* create a new row for that  user*/
				$this->db->insert('daily_attendance_sheet',$where);
						
						/*update all sundays*/
						foreach ($sunday as $s){
						$insert_sunday['day_'.$sundays] = 'sunday';
						$this->db->update('daily_attendance_sheet',$insert_sunday,$where);	
							}
				
						}	
						
					}
		}
		
				
		$this->db->update('user_leave',array('status'=>'verified','remarks'=>$updates['remarks']),array('id'=>$id));
	
		$this->db->close();
	} 
}
	
	public function paid_leaves_taken($id){		
		$this->db->select('sum(paid_leave_taken) as pl');
		$this->db->from('salary_details');
		$this->db->where('user_id',$id);
		return $this->db->get()->row_array();		
	}
	
	public function total_paid_leaves($id){
		$this->db->select('paid_leave');
		$this->db->from('salary_account');
		$this->db->where('user_id',$id);
		return $this->db->get()->row_array();
		$this->db->close();
	}
	
	function get_sunday($month_id,$year_id)
	{
		 $lastday = cal_days_in_month(CAL_GREGORIAN, $month_id, '20'.$year_id);
		 		
		$start   		=  '20'.$year_id.'-'.$month_id.'-01';
		$end           	= '20'.$year_id.'-'.$month_id.'-'.$lastday;
		$startDate     = strtotime($start);
		$endDate       = strtotime($end);
		$weekdayNumber = 0;
		$dateArr       = array();
		do
		{
			if(date("w", $startDate) != $weekdayNumber){
				$startDate += (24 * 3600); // add 1 day
			}
		} while(date("w", $startDate) != $weekdayNumber);


		while($startDate <= $endDate){
			$dateArr[] = date('d', $startDate);
			$startDate += (7 * 24 * 3600); // add 7 days
		}

		return($dateArr);		
	}
	
}
